草庐IT

Android PNG 到位图 --- SkImageDecoder::Factory 返回 null

全部标签

c# - string str 和 string str=null 的区别

我想知道当我们声明一个变量时内部到底发生了什么,就像这样:stringtr;stringtr=null;在调试时,我注意到这两个值都只显示空值。但是当使用reftr而不初始化null时,它会给出错误,而第二行不会。请帮助我深入了解它 最佳答案 你的第一条语句只是声明,你的第二条语句是声明+初始化。stringtr;//JustDeclarationstringtr=null;//Declaration+Initialization.如果您尝试仅在声明中使用tr,您可能会遇到编译时错误。(第一种情况)例如:stringtr;//Jus

c# - 为 C# 方法添加返回语句会提高性能吗?

这blog说12)IncludeReturnStatementswithintheFunction/Method.HowitimprovesperformanceExplicitlyusingreturnallowstheJITtoperformslightlymoreoptimizations.Withoutareturnstatement,eachfunction/methodisgivenseverallocalvariablesonstacktotransparentlysupportreturningvalueswithoutthekeyword.Keepingthesear

c# - 如果参数值为 null,则传递字符串

我需要在一个函数中传递多个参数。我的要求是参数值不能为NULL。如果参数为NULL,改为传递“TBD”。例如getBookInfo(stringbookId,stringbookName,stringbookAuthor)//ifanyoftheparametersisNULL,pass"TBD"stringinparameter我该怎么做?我可以使用三元运算符来做到这一点吗?如果可以,怎么做? 最佳答案 在调用您的方法时尝试这样做:getBookInfo(bookId??"TBD",bookName??"TBD",bookAuth

c# - ASP.NET Web API - 返回 CLR 对象或 HttpResponseMessage

对于操作方法的返回类型,WebAPI中的一般做法似乎是什么?像这样返回CLR对象:publicIEnumerableGet(){return_contactService.GetAllForUser();}或者将您的对象包装在HttpResponseMessage中:publicHttpResponseMessageGet(){IEnumerablecontacts=_contactService.GetAllForUser();returnRequest.CreateResponse((HttpStatusCode)200,contacts);}我更喜欢将我自己的CLR对象作为返回类

c# - 我应该对每个返回任务的方法使用异步/等待吗

这个问题在这里已经有了答案:Whyuseasyncandreturnawait,whenyoucanreturnTaskdirectly?(8个答案)关闭4个月前。假设我有一个C#Controller,它调用某个返回任务的任意函数(例如,因为它执行数据库事务)。我应该始终使用async和await,还是应该只返回任务?示例Controller:publicasyncTaskDoSomething(){returnawaitSomeOtherFunctionThatReturnsATask();}我应该把它改成:publicTaskDoSomething(){returnSomeOthe

c# - Uri.TryCreate 对任何字符串值返回 true?

我尝试使用Uri.TryCreate方法验证Uri,当我使用无效字符串调用它时,该方法返回true。任何想法为什么?我的代码如下:privatevoidbutton1_Click(objectsender,EventArgse){UritempValue;if(Uri.TryCreate("NotAURL",UriKind.RelativeOrAbsolute,outtempValue)){MessageBox.Show("?");}} 最佳答案 这是一个有效的相对URL。一个无效URI的例子是:"http://example.co

c# - 将 null 分配给可为空的 int 时出错 - "The value ' null' 对属性无效”

我的View模型中有这个属性:[DisplayName("Region")]publicint?RegionId{get;set;}我将我的View模型传递给我的Controller,如果RegionId为null,它会在ModelState.IsValid处失败。如果我向它传递一个整数,它就可以正常工作。错误信息是:Thevalue'null'isnotvalidforRegion在检查ModelState.IsValid之前,我也试过调用它,但我得到了同样的错误:if(viewModel.RegionId==null)viewModel.RegionId=(int?)null;这里

c# - 在 TPL 中返回一个空的静态任务是一种不好的做法吗?

有些情况下我想有条件地运行任务。我使用这样的某种扩展方法:publicstaticclassMyTaskExtension{privatestaticTasktheEmptyTask=Task.Factory.StartNew(()=>{});//ThisisthequestionpublicstaticTaskContinueWith(thisTasktask,TaskcontinuationTask,Funccondition){ifcondition(){...dothework}returntheEmptyTask;}}我的期望是theEmptyTask已经完成,所以基本上如果

c# - 如何知道我的 linq 查询是否返回 null

我有这个linq查询:varmyQuery=fromQinmyDataContextselectQ.Name当我尝试这样做时:listView.ItemsSource=myQuery它有时会抛出异常,因为myQuery中没有元素我尝试了很多方法,例如:if(myQuery.count!=0)或if(myQuery.Any())但没有任何效果,那么我如何确定我的查询是否返回空值? 最佳答案 您可以将结果实现为列表:varmyQuery=(fromQinmyDataContextselectQ.Name).ToList();现在您可以查

c# - Image.Save 崩溃 : {"Value cannot be null.\r\nParameter name: encoder"}

我正在尝试将图像保存到MemoryStream中,但在某些情况下会失败。代码如下:以下代码成功:Imageimg=Bitmap.FromStream(fileStream);MemoryStreamms=newMemoryStream();img.Save(ms,img.RawFormat);//Thissucceeds.以下代码失败:Imageimg=Bitmap.FromStream(fileStream);Imagethumb=img.GetThumbnailImage(thumbWidth,thumbHeight,null,System.IntPtr.Zero);MemoryS